package org.magicbox.qjunTPO.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.magicbox.qjunTPO.common.lang.Result;
import org.magicbox.qjunTPO.entity.Supuser;
import org.magicbox.qjunTPO.entity.User;
import org.magicbox.qjunTPO.service.SupuserService;
import org.magicbox.qjunTPO.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.print.attribute.IntegerSyntax;
import java.time.LocalDateTime;
import java.util.ArrayList;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    UserService userService;

    @Autowired
    SupuserService supuserService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {  //自定义登录逻辑
        User user;
        if(username.length()==10){  //学号登录
            Integer stuid = new Integer(username);
            user = userService.getOne(new QueryWrapper<User>().eq("stuid", stuid));
        }else if(username.length()==11){    //手机号登录
            System.out.println(username);
            user = userService.getOne(new QueryWrapper<User>().eq("phone_number",username));
        }
        else if (username.length() == 4){
            Integer suid = new Integer(username);
            Supuser supuser = supuserService.getById(suid);
            String power = SuperPowerString(supuser);
            return new org.springframework.security.core.userdetails.User(
                    supuser.getSuId().toString(),
                    supuser.getPassword(), AuthorityUtils.commaSeparatedStringToAuthorityList(power));
        }
        else
        {
            throw new UsernameNotFoundException("用户不存在");
        }
        user.setLastDate(LocalDateTime.now());
        switch (user.getMarker()){
            case 0:
                return new org.springframework.security.core.userdetails.User(
                        user.getId().toString(),
                        user.getPassword(), AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_LV0"));
            case 1:
                return new org.springframework.security.core.userdetails.User(
                        user.getId().toString(),
                        user.getPassword(), AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_LV1"));
            case 2:
                return new org.springframework.security.core.userdetails.User(
                        user.getId().toString(),
                        user.getPassword(), AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_LV2"));
            case 3:
                return new org.springframework.security.core.userdetails.User(
                        user.getId().toString(),
                        user.getPassword(), AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_Stop"));
            case 4:
                return new org.springframework.security.core.userdetails.User(
                        user.getId().toString(),
                        user.getPassword(), AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_LV3"));
            default:
                throw new UsernameNotFoundException("用户标识非法");
        }
    }

    public String SuperPowerString(Supuser supuser){
//        String power = "";
        ArrayList<String> powerList = new ArrayList<>();
        if (supuser.getSupuserOk().equals(1)) {
            powerList.add("SuperPower");
        }
        if(supuser.getUserOk().equals(1)) powerList.add("UserPower");
        if(supuser.getEventOk().equals(1)) powerList.add("EventPower");
        if (supuser.getDeskOk().equals(1)) powerList.add("DeskPower");
        if (supuser.getPoemOk().equals(1)) powerList.add("PoemPower");
        String s = powerList.toString();
        return s.substring(1,s.length()-1);
    }
}
